import {
  IsString,
  IsNotEmpty,
  IsOptional,
  IsNumber,
  IsArray,
  IsEnum,
  MaxLength,
  Min,
  Max
} from 'class-validator';
import { ApiProperty, ApiPropertyOptional, PartialType } from '@nestjs/swagger';
import { Type } from 'class-transformer';

// 创建角色 DTO
export class CreateRoleDto {
  @ApiProperty({
    description: '角色名称',
    example: 'editor',
    maxLength: 50
  })
  @IsString()
  @IsNotEmpty()
  @MaxLength(50)
  name: string;

  @ApiProperty({
    description: '角色标签',
    example: '编辑者',
    maxLength: 100
  })
  @IsString()
  @IsNotEmpty()
  @MaxLength(100)
  label: string;

  @ApiPropertyOptional({
    description: '角色描述',
    example: '负责内容编辑和审核',
    maxLength: 255
  })
  @IsOptional()
  @IsString()
  @MaxLength(255)
  description?: string;

  @ApiPropertyOptional({
    description: '状态',
    example: 1,
    enum: [0, 1],
    default: 1
  })
  @IsOptional()
  @IsEnum([0, 1])
  status?: number;

  @ApiPropertyOptional({
    description: '排序',
    example: 0,
    default: 0
  })
  @IsOptional()
  @IsNumber()
  sort?: number;

  @ApiPropertyOptional({
    description: '权限列表',
    type: [String],
    example: ['posts:read', 'posts:write', 'users:read']
  })
  @IsOptional()
  @IsArray()
  @IsString({ each: true })
  permissions?: string[];
}

// 更新角色 DTO
export class UpdateRoleDto extends PartialType(CreateRoleDto) {}

// 查询角色 DTO
export class QueryRolesDto {
  @ApiPropertyOptional({
    description: '页码',
    example: 1,
    minimum: 1,
    default: 1
  })
  @IsOptional()
  @Type(() => Number)
  @IsNumber()
  @Min(1)
  page?: number = 1;

  @ApiPropertyOptional({
    description: '每页数量',
    example: 10,
    minimum: 1,
    maximum: 100,
    default: 10
  })
  @IsOptional()
  @Type(() => Number)
  @IsNumber()
  @Min(1)
  @Max(100)
  limit?: number = 10;

  @ApiPropertyOptional({
    description: '搜索关键词（角色名、标签、描述）',
    example: 'admin'
  })
  @IsOptional()
  @IsString()
  search?: string;

  @ApiPropertyOptional({
    description: '状态',
    enum: [0, 1]
  })
  @IsOptional()
  @Type(() => Number)
  @IsEnum([0, 1])
  status?: number;

  @ApiPropertyOptional({
    description: '排序字段',
    example: 'sort',
    enum: ['createdAt', 'updatedAt', 'name', 'sort']
  })
  @IsOptional()
  @IsString()
  sortBy?: string = 'sort';

  @ApiPropertyOptional({
    description: '排序方向',
    example: 'ASC',
    enum: ['ASC', 'DESC'],
    default: 'ASC'
  })
  @IsOptional()
  @IsString()
  sortOrder?: 'ASC' | 'DESC' = 'ASC';
}

// 分配权限 DTO
export class AssignPermissionsDto {
  @ApiProperty({
    description: '权限列表',
    type: [String],
    example: ['posts:read', 'posts:write', 'users:read']
  })
  @IsArray()
  @IsString({ each: true })
  permissions: string[];
}

// 批量操作角色 DTO
export class BatchRoleActionDto {
  @ApiProperty({
    description: '角色ID数组',
    type: [Number],
    example: [1, 2, 3]
  })
  @IsArray()
  @IsNumber({}, { each: true })
  roleIds: number[];

  @ApiProperty({
    description: '操作类型',
    enum: ['activate', 'deactivate', 'delete']
  })
  @IsString()
  @IsEnum(['activate', 'deactivate', 'delete'])
  action: 'activate' | 'deactivate' | 'delete';
}
